/**
 * Created by 123 on 2017/12/7.
 * 问题集
 */
import {queryTroubleCollection, queryTroubleCollectionItem} from '../services/troubleCollection';
import {Toast} from 'antd-mobile'

function showToast(msg) {
  Toast.fail(msg, 1)
}
export default {
  namespace: 'troubleCollection',

  state: {
    troubles: [],//问题集分类
    normList: [],//问题
    checkList: [],
    seriousGrade: [],
    hasChange: false,
    name:'',
    thisTypeId:'',
      loading:true,
  },

  effects: {
    //获取问题集
    *queryTroubleCollection({payload}, {call, put,select}) {
        yield put({type:'getLoading'});
      const {typeId} = yield select(state => state.rectifyBill);
      const {thisTypeId,normList,name} = yield select(state => state.troubleCollection);
      const {res, error} = yield call(queryTroubleCollection);
      if (error) {
          yield put({type: 'cleanLoading'})
        yield put({type: 'queryTroubleCollectionFail', payload: error})
      } else {
        if (res.code == 1) {
          yield put({type: 'queryTroubleCollectionSuccess', payload: {res:res,typeIdNew:typeId,
            typeIdOld:thisTypeId,normList:normList,name:name}})
        } else {
            yield put({type: 'cleanLoading'})
          yield put({type: 'queryTroubleCollectionFail', payload: res.message})
          showToast(res.message)
        }
      }
    },
    //获取问题
    *queryTroubleCollectionItem({payload}, {call, put, select}) {
      const {res, error} = yield call(queryTroubleCollectionItem, {pageNo: 1, pageSize: 10, categoryId: payload.id});
      if (error) {
        yield put({type: 'queryItemFail', payload: error})
      } else {
        if (res.code == 1) {
          yield put({type: 'queryItemSuccess', payload: {res:res,name:payload.name}})
        } else {
          yield put({type: 'queryItemFail', payload: res.message})
          showToast(res.message)
        }
      }
    },
    //将问题发送到整改单
    *setTroubles({payload}, {call, put, select}){
      const {troubleWhere}=yield select(state => state.rectifyBill);
      const {normList}=yield select(state => state.troubleCollection);
      let list = [];
      for (let i = 0; i < normList.length; i++) {
        if (normList[i].thisChecked) {
          let param = {};
          param.id = normList[i].id;
          param.content = normList[i].content+'\u3000'+normList[i].importLevel;
          param.level = normList[i].importLevel;
          list.push(param)
        }
      }
      yield put({type: 'rectifyBill/collectionTroubles', payload: {data: list, typeId: troubleWhere}})
    },

    *thisBoxChecked({payload}, {call, put, select}){
      const {normList}=yield select(state => state.troubleCollection);
      let thisList = normList;
      for (let i = 0; i < thisList.length; i++) {
        if (thisList[i].id === payload.id) {
          if (thisList[i].thisChecked) {
            thisList[i].thisChecked = false;
          } else {
            thisList[i].thisChecked = true;
          }
        }
      }
      yield put({type: 'troubleCollection/checkThis', payload: {list: thisList}})
    },

  //  全选
    *allCheck({payload}, {call, put, select}){
      const {normList}=yield select(state => state.troubleCollection);
      yield put({type: 'allCheckThis', payload: {list: normList,thisName:payload}})
    },

      *getClean({payload}, {call, put, select}){
          yield put({type: 'getThisClean'})
      },
  },

  reducers: {
    queryTroubleCollectionFail(state, {payload}) {

      return {...state,}
    },

    queryTroubleCollectionSuccess(state, {payload}) {
      if(payload.typeIdNew === payload.typeIdOld){

      }else {
        payload.normList = [];
        payload.name = '';
      }
      return {...state, troubles: payload.res.data,thisTypeId:payload.typeIdNew,
        normList:[].concat(payload.normList),name:payload.name,loading:false}
    },

    queryItemFail(state, {payload}) {
      return {...state,}
    },

    queryItemSuccess(state, {payload}) {
      let res = payload.res;
      let name = payload.name;
      let data = [];
      let ser = [];
      if (res.data.list[0]) {
          res.data.list[0].seriousGrade.map((val, index) => {
          ser.push({label: val.label, value: val.label})
        });
      }
        res.data.list.map((key, val) => {

        data.push({
          thisChecked: false,
          content: key.name, importLevel: key.gradeName,
          according: key.according, gradeName: key.gradeName,
          id: key.id,
        })
      });

      return {...state, normList: data, seriousGrade: ser,name:name}
    },
    changeGradeName(state, {payload}){
      let list = state.normList;
      list.map((val, index) => {
        if (index == payload.typeId) {
          val.gradeName = payload.data;
          val.importLevel = payload.data;
        }
      });
      return {...state, normList: list, hasChange: !state.hasChange}
    },

    checkThis(state, {payload}){
      return {...state, normList: [].concat(payload.list)}
    },

    allCheckThis(state, {payload}){
      let thisList = payload.list;
      let allName = '';
      if (payload.thisName === undefined || payload.thisName === '全选'){
        for (let i = 0; i < thisList.length; i++) {
          thisList[i].thisChecked = true;
        }
        allName = '取消全选'
      }else if(payload.thisName === '取消全选') {
        for (let i = 0; i < thisList.length; i++) {
          thisList[i].thisChecked = false;
        }
        allName='全选'
      }
      return {...state, allCheck:allName,normList:[].concat(thisList)}
    },
    resetNormalList(state, {payload}){
      return {...state, normList: [],name:'',loading:true}
    },
      getLoading(state,{payload}){
          return{...state,loading:true}
      },
      cleanLoading(state,{payload}){
          return{...state,loading:false}
      },
      getThisClean(state,{payload}){
          return{...state,
              troubles: [],
              normList: [],
              checkList: [],
              seriousGrade: [],
              hasChange: false,
              name:'',
              thisTypeId:'',
              loading:true,
          }
      },
  },


}